<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 微信自定义菜单
 * @package App\Http\Controllers\Admin
 */
class WxMenuController extends BaseController
{
    private $wxMenuSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->wxMenuSer = new \App\Services\WxMenuService();
    }

    /**
     * 微信菜单列表
     * @return mixed
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            //菜单列表
            $menulist = $this->wxMenuSer->getMenuList()->toArray();
            if ($menulist) {
                foreach ($menulist as $k => $v) {
                    $v['text'] = $v['name'];
                    $v['icon'] = 'fa fa-yelp';
                    $v['tags'] = [
                        '排序：' . $v['order_num'],
                        '个性化菜单：' . ($v['is_conditional'] == 1 ? '是' : '否'),
                        '菜单类型：' . $this->wxMenuSer->getTypeName($v['type']),
                    ];
                    $menulist[$k] = $v;
                }
            }
            $tree = new \App\Services\TreeService('menu_id', 'parent_id', 'nodes');
            $tree->load($menulist);
            $treeMenuList = $tree->deepTree();
            $data['treeList'] = $treeMenuList ? $treeMenuList : [];
            return $data;
        }
        //菜单类别
        $typeList = $this->wxMenuSer->getTypeList();
        $this->viewData['typeList'] = $typeList;
        //性别列表
        $sexList = $this->wxMenuSer->getSexList();
        $this->viewData['sexList'] = $sexList;
        //客户端列表
        $clientPlatformList = $this->wxMenuSer->getClientPlatformList();
        $this->viewData['clientPlatformList'] = $clientPlatformList;
        //标签选择项
        $wxTagSer = new \App\Services\WxUserTagService();
        $tagOptionHtml = $wxTagSer->getSelectOptionHtml();
        $this->viewData['tagOptionHtml'] = $tagOptionHtml;
        //语言列表
        $langList = $this->wxMenuSer->getLanguageList();
        $this->viewData['langList'] = $langList;
        //地区数据
        $wxAreaSer = new \App\Services\WxAreaService();
        $this->viewData['areaList'] = $wxAreaSer->getChunkListByParentId();
        return view('admin.wxmenu.index', $this->viewData);
    }

    /**
     * 保存菜单
     * @param Request $request
     * @return mixed
     */
    public function save(\App\Http\Requests\AdminWxMenuPost $request)
    {
        try {
            $this->wxMenuSer->save($request);
            return $this->showMsg('success', '保存菜单成功', 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除菜单
     * @param Request $request
     * @return mixed
     */
    public function destroy(Request $request)
    {
        try {
            $this->wxMenuSer->del($request->id ?? 0);
            return $this->showMsg('success', '删除菜单成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 菜单发布
     * @param Request $request
     * @return mixed
     */
    public function publish()
    {
        try {
            $this->wxMenuSer->publish();
            return $this->showMsg('success', '发布菜单成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 菜单同步
     * @return mixed
     */
    public function sync()
    {
        $this->wxMenuSer->sync();
        try {
            return $this->showMsg('success', '同步菜单成功', [], 'admin.wxuser.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
